import { FairyEditor, FairyGUI } from "csharp";
import Global from "../Global";
import UIBaseWindow from "./UIBaseWindow";


export default class GameLayer {

    // public static sceneLayer: GameUI;

     /**场景提示层 */
    public static fightLayer: FairyGUI.GComponent;
    /**主界面层 */
    public static mainLayer: FairyGUI.GComponent;
    /**界面层 */
    public static windowLayer: FairyGUI.GComponent;
    /**显示层 */
    public static visibleLayer: FairyGUI.GComponent;
    /**弹框层 */
    public static alertLayer: FairyGUI.GComponent;    
    /**货币飞行层 */
    public static flayLayer: FairyGUI.GComponent;
    /**顶层 */
    public static topLayer: FairyGUI.GComponent;

    /**开放域层 */
    public static openLayer:FairyGUI.GComponent;
    /**广告层 */
    public static adLayer:FairyGUI.GComponent;
    
    public static start(): void  {

        // fgui.AddChild(FairyGUI.GRoot.inst.displayObject);

        Global.stage = FairyGUI.GRoot.inst.displayObject.stage;

        GameLayer.fightLayer = new FairyGUI.GComponent();
        GameLayer.mainLayer = new FairyGUI.GComponent();
        GameLayer.windowLayer = new FairyGUI.GComponent();
        GameLayer.visibleLayer = new FairyGUI.GComponent();
        GameLayer.alertLayer = new FairyGUI.GComponent();
        GameLayer.flayLayer = new FairyGUI.GComponent();
        GameLayer.topLayer = new FairyGUI.GComponent();
        GameLayer.openLayer = new FairyGUI.GComponent();
        GameLayer.adLayer = new FairyGUI.GComponent();
        
        FairyGUI.GRoot.inst.AddChild(GameLayer.fightLayer);
        FairyGUI.GRoot.inst.AddChild(GameLayer.mainLayer);
        FairyGUI.GRoot.inst.AddChild(GameLayer.windowLayer);
        FairyGUI.GRoot.inst.AddChild(GameLayer.visibleLayer);
        FairyGUI.GRoot.inst.AddChild(GameLayer.alertLayer);
        FairyGUI.GRoot.inst.AddChild(GameLayer.flayLayer);
        FairyGUI.GRoot.inst.AddChild(GameLayer.topLayer);
        FairyGUI.GRoot.inst.AddChild(GameLayer.openLayer);
        FairyGUI.GRoot.inst.AddChild(GameLayer.adLayer);

        // FairyGUI.UIConfig.defaultFont = GameConfig.defaultFont;
        // FairyGUI.UIConfig.packageFileExtension = "bin";        

        // Laya.stage.on(Laya.Event.RESIZE, GameLayer, GameLayer.onResize);
        Global.stage.onStageResized.Add( (context: FairyGUI.EventContext) => {
            GameLayer.onResize();
        });
        this.onResize();
    }

    public static onResize(): void  {

        
        Global.stageWidth = FairyEditor.App.groot.viewWidth;
        Global.stageHeight = FairyEditor.App.groot.viewHeight;

        GameLayer.onUpdate(GameLayer.fightLayer);
        GameLayer.onUpdate(GameLayer.mainLayer);
        GameLayer.onUpdate(GameLayer.windowLayer);
        GameLayer.onUpdate(GameLayer.visibleLayer);
        GameLayer.onUpdate(GameLayer.alertLayer);
        GameLayer.onUpdate(GameLayer.flayLayer);
        GameLayer.onUpdate(GameLayer.topLayer);
        GameLayer.onUpdate(GameLayer.openLayer);
        GameLayer.onUpdate(GameLayer.adLayer);        
    }
    
    private static onUpdate(parent: FairyGUI.GComponent): void  {
        let length = parent.numChildren;
        var view: UIBaseWindow = null;
        for (var i: number = 0; i < length; i++)  {
            view = parent.GetChildAt(i) as UIBaseWindow;
            if (view != null )  {
                view.onResize();
            }
        }
    }

}